<?php
/**
 * Created by PhpStorm.
 * User: yangxu
 * Date: 2016/5/4
 * Time: 9:56
 */

namespace Admin\Controller;
use Think\Controller;

/**
 * Class CategoryController
 * @package Admin\Controller
 * 作用： 题库分类
 */
class CategoryController extends Controller
{   //分类表变量
    private $cat;
    public function __construct(){
        parent::__construct();
        if(!logAdmin(session('adminId'))) header('location:'.U('Admin/Index/login'));
        //实例化分类表
        $this->cat=M('Category');
    }

    /**
     * @action index
     * 作用：分类列表
     */
    public function index(){
        $pid=isset($_GET['pid'])?$_GET['pid']:0;
        $type=isset($_GET['type'])?$_GET['type']:null;
        //查询所有分类
        $this->assign('pid',$pid);
        $this->assign('type',$type);
        if($type != null){
            $where['type']=$type;
        }
        $where['parent_id']=$pid;
        $catList=$this->cat->where($where)->select();
        //引入树形结构类
//        vendor('Tree');
//        //实例化树形结构类
//        $tree=new \Tree();
//        //初始化数据
//        $tree->tree($list);
//        //得到树形结构
//        $catList=$tree->getRet();
        foreach ($catList as $key => $val){
            if($val['level']==2){
                $catList[$key]['status']=1;
            }
            if($val['level']>2){
                $catList[$key]['tiliang']=1; 
            }
        }
        $this->assign('catList',$catList);
        $this->display();
    }

    /**
     * @action addCat
     * 作用：添加分类
     */
    public function addCat(){
        $pid=$_GET['pid'];
        $type=isset($_GET['type'])?$_GET['type']:null;
        //查询上级等级
        $catli=$this->cat->field('level')->where('id='.$pid)->find();
        if($_POST){
            //生成插入数据
            $data=array(
                'parent_id'=>$pid,
                'catname'=>trim($_POST['catName']),
                'level'=>((int)$catli['level']+1)
            );
            if(isset($_POST['total'])){
                $data['total']= $_POST['total'];
            }
            if(!empty($type)){
                $data['type']= $type;
            }
            //插入数据
            if($id=$this->cat->add($data)){
                if(isset($_POST['price'])){
                    $price=M('Price');
                    $priceli=$price->where('cate_id='.$id)->find();
                    if(empty($priceli)){
                        $price->add(array(
                            'exam_price'=>$_POST['price'],
                            'cate_id'=>$id,
                            'reference_price'=>0,
                            'testkey_price'=>0,
                        ));
                    }else{
                        $price->where('cate_id='.$_GET['id'])->setField(array('exam_price'=>$_POST['price']));
                    }
                }
                $this->success('成功',U('index'));
            }else{
                $this->error('失败');
            }
        }else{
            if($catli['level'] == 1){
                $this->assign('status',1);
            }else{
                $this->assign('status',0);
            }
            
            $this->display('add');
        }

    }

    /**
     * @action editCat
     * 作用：编辑分类
     */
    public function editCat(){
        if($_POST){
            //生成数据
            $data=array(
                'id'=>$_GET['id'],
                'catname'=>trim($_POST['catName']),
            );
            if(isset($_POST['total'])){
               $data['total']= $_POST['total'];
            }
            if(isset($_POST['price'])){
                $price=M('Price');
                $price->where('cate_id='.$_GET['id'])->setField(array('exam_price'=>$_POST['price']));
            }
            //编辑操作
            $this->cat->save($data);
                $this->success('成功',U('index'));
            
        }else{
            //查询分类详情
            $price=M('Price');
            $catinfo=$this->cat->where('id='.$_GET['id'])->find();
            $priceli=$price->field('exam_price')->where('cate_id='.$_GET['id'])->find();
            if(empty($priceli)){
                $price->add(array(
                    'exam_price'=>0,
                    'cate_id'=>$_GET['id'],
                    'reference_price'=>0,
                    'testkey_price'=>0,
                ));
                $catinfo['price']=0;
            }else{
                $catinfo['price']=$priceli['exam_price'];
            }

            $this->assign('catinfo',$catinfo);
            $this->display('edit');
        }

    }

    /**
     * 删除分类
     */
    public function catDel(){
        $num=$this->cat->where('parent_id='.$_GET['id'])->count();
        if($num>0){
            $this->error('删除失败,该分类下存在子分类！');
        }else{
            if($this->cat->where('id='.$_GET['id'])->delete()){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }

    }

}